package io.github.jesse205.dragdrop.pro.view

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.ViewGroup
import android.widget.FrameLayout
import com.google.android.material.bottomnavigation.BottomNavigationView

/**
 * 修复布局波纹溢出
 * */
class BottomNavigationView : BottomNavigationView {
    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init()
    }

    constructor(
        context: Context,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes) {
        init()
    }

    private fun init() {
        wrapChildrenToContainer()
    }

    /**
     * 包裹所有子视图到容器内，放置无边框波纹溢出到导航栏区域
     * */
    private fun wrapChildrenToContainer() {
        val container = FrameLayout(context)
        for (index in 0 until childCount) {
            getChildAt(index).apply {
                removeView(this)
                container.addView(this)
            }
        }
        container.setBackgroundColor(Color.TRANSPARENT)
        addView(container, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
    }
}